package com.itheima.edu.info.manager.dao.impl;


import com.itheima.edu.info.manager.dao.BaseStudentDao;
import com.itheima.edu.info.manager.domain.Person;
import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.utils.FileUtils;

import java.io.*;
import java.util.ArrayList;

public class OtherStudentDao implements BaseStudentDao {
    static final String FILE_PATH = "itheima-manager-standard-edition";

    //创建长度为5的学生数组对象容器   static修饰的成员可以被多个StudentDao对象调用
    private static ArrayList<Student> students = new ArrayList<>();
    static {
        //静态方法块,初始化数据
        //reload();
        students = (ArrayList<Student>) FileUtils.reSaveByObjectReaderList(new File("itheima-manager-standard-edition//studentdao.txt"));

        System.out.println("----------student集合初始化完成----------");
    }

    public Student[] findAllStudents() {
        Student[] stus = new Student[students.size()];
        for (int i = 0; i < stus.length; i++) {
            stus[i] = students.get(i);
        }
        return stus;
    }

    public boolean addStudent(Student student) {
        students.add(student);
        //reSave();
        FileUtils.reloadByObjectWriterList(students,new File("itheima-manager-standard-edition//studentdao.txt"));
        return true;
    }

    public boolean delectStudent(String id) {
        //遍历学生数组
        for (int i = 0; i < students.size(); i++) {
            if ( students.get(i).getId().equals(id)) {
                students.remove(i);
                //reSave();
                FileUtils.reloadByObjectWriterList(students,new File("itheima-manager-standard-edition//studentdao.txt"));
                return true;
            }
        }
        //不存在返回false
        return false;
    }

    public Student selectStudent(String id) {
        int index = -1;
        //遍历学生数组
        for (int i = 0; i < students.size(); i++) {
            if ( students.get(i).getId().equals(id)) {
                index = i;
                break;
            }
        }

        return students.get(index);
    }

    public Student updataStudent(Student student) {
        Student stu = new Student();
        int index = -1;
        //遍历学生数组
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getId().equals(student.getId())) {
                students.get(i).setName(student.getName());
                students.get(i).setAge(student.getAge());
                students.get(i).setBirthday(student.getBirthday());
                index = i;
                //reSave();
                FileUtils.reloadByObjectWriterList(students,new File("itheima-manager-standard-edition//studentdao.txt"));
                 break;
            }
        }

        return students.get(index);
    }
    /*
    //封装一个方法将内存中的数据写入磁盘     === BufferedWriter
    public void reSave(){
        System.out.println("resave...");
        BufferedWriter bufferedWriter = null;
        try {
            //定义输出流对象
            bufferedWriter = new BufferedWriter(new FileWriter(new File(FILE_PATH , "user.txt")));
            //循环读写
            for (Student stu : students) {
                bufferedWriter.write(stu.toString());
                bufferedWriter.newLine();
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    //e.printStackTrace();
                }
            }
        }
    }

    //封装一个方法将硬盘中的数据加载进内存    == BufferedReader
    public static void reload(){
        System.out.println("reload...");
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new FileReader(new File(FILE_PATH , "user.txt")));

            //循环读写
            String stuArr;
            while ((stuArr = bufferedReader.readLine()) != null){
                String[] split = stuArr.split("\t");
                students.add(new Student(split[0],split[1],split[2],split[3]));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (bufferedReader != null){
                    bufferedReader.close();
                }
            }catch (Exception e){
               // e.printStackTrace();
            }
        }
    }

    //序列化于反序列化流
*/
}
